/*
 * Powered By XY
 * Since 2020
 */

package com.xy.biz.rs.service.impl;

import java.time.LocalDateTime;
import java.util.Collection;
import java.util.List;
import java.util.Objects;

//import com.xy.biz.dd.client.DataTemplateClient;
//import com.xy.biz.dd.domain.req.GetDataTemplateRequest;
//import com.xy.biz.dd.domain.req.SaveDataCellRequest;
//import com.xy.biz.dd.domain.req.SaveDataTableRequest;
//import com.xy.biz.dd.domain.resp.GetDataCellResponse;
//import com.xy.biz.dd.domain.resp.GetDataTemplateResponse;
import com.xy.biz.fd.client.FolderClient;
import com.xy.biz.fd.domain.req.folder.CreateManufactureFolderRequest;
import com.xy.biz.fd.domain.resp.GetFolderResponse;
import com.xy.biz.rs.domain.converter.ManufactureRecordTemplateConverter;
import com.xy.biz.rs.domain.req.GetByProductDictCodeRequest;
import com.xy.biz.rs.domain.req.SaveManufactureRecordTemplateRequest;
import com.xy.biz.rs.domain.req.UpdateManufactureRecordTemplateRequestStatus;
import com.xy.biz.rs.domain.resp.GetManufactureRecordTemplateResponse;
import com.xy.tool.context.UserContextHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.xy.lang.domain.Result;
import com.xy.biz.rs.domain.bo.ManufactureRecordTemplateBO;
import com.xy.biz.rs.domain.query.ManufactureRecordTemplateQuery;
import com.xy.biz.rs.service.ManufactureRecordTemplateService;
import com.xy.biz.rs.manager.ManufactureRecordTemplateManager;
import org.springframework.util.CollectionUtils;

/**
 *
 *
 * @author zsp
 * @date 2021-4-13
 */
@Service
public class ManufactureRecordTemplateServiceImpl implements ManufactureRecordTemplateService {

	@Autowired
	private ManufactureRecordTemplateManager manufactureRecordTemplateManager;

//	@Autowired
//	private DataTemplateClient dataTemplateClient;

	@Autowired
	private FolderClient folderClient;

	@Override
	public Result<?> saveManufactureRecordTemplate(ManufactureRecordTemplateBO manufactureRecordTemplateBO) {


		//通过子项查询是否已存在
		ManufactureRecordTemplateQuery query = ManufactureRecordTemplateQuery.one()
				.subProductDictCode(manufactureRecordTemplateBO.getSubProductDictCode())
				.build();
		int row = manufactureRecordTemplateManager.countManufactureRecordTemplate(query);
		if (row > 0) {
			return Result.ofConflict("产品记录模板已存在");
		}

		//创建项目文件夹
		Result<GetFolderResponse> manufactureFolder = folderClient.createManufactureFolder(CreateManufactureFolderRequest.builder()
				.subProductDictCodeFolderName(manufactureRecordTemplateBO.getSubProductDictCode())
				.repositoryId(manufactureRecordTemplateBO.getRepositoryId()).build());
		if (!manufactureFolder.wasSuccess()) {
			return Result.ofBadRequest("创建文件夹失败");
		}
		GetFolderResponse data = manufactureFolder.getData();

		manufactureRecordTemplateBO.setFolderId(data.getFolderId());
		LocalDateTime now = LocalDateTime.now();
		manufactureRecordTemplateBO.setStatus(0);
		manufactureRecordTemplateBO.setTenantId(UserContextHolder.getTenantId());
		manufactureRecordTemplateBO.setCreateTime(now);
		manufactureRecordTemplateBO.setUpdateTime(now);
		//manufactureRecordTemplateBO = manufactureRecordTemplateManager.saveManufactureRecordTemplate(manufactureRecordTemplateBO);
		if (Objects.nonNull(manufactureRecordTemplateBO)) {
			return Result.success(ManufactureRecordTemplateConverter.toResponse(manufactureRecordTemplateBO));
		}
		return Result.ofErrorRowsAffected();
	}

	@Override
	public Result<?> updateManufactureRecordTemplate(SaveManufactureRecordTemplateRequest request) {
		ManufactureRecordTemplateBO manufactureRecordTemplate = manufactureRecordTemplateManager.getManufactureRecordTemplate(request.getManufactureRecordTemplateId());

		ManufactureRecordTemplateBO manufactureRecordTemplateBO = ManufactureRecordTemplateConverter.toBO(request);
		if (!manufactureRecordTemplate.getRepositoryId().equals(request.getRepositoryId())) {
			Result<GetFolderResponse> manufactureFolder = folderClient.createManufactureFolder(CreateManufactureFolderRequest.builder()
					.subProductDictCodeFolderName(request.getSubProductDictCode())
					.repositoryId(request.getRepositoryId()).build());
			if (!manufactureFolder.wasSuccess()) {
				return Result.ofBadRequest("创建文件夹失败");
			}
			GetFolderResponse data = manufactureFolder.getData();
			manufactureRecordTemplateBO.setFolderId(data.getFolderId());
		}
		manufactureRecordTemplateBO.setUpdateTime(LocalDateTime.now());
		int affectedRows = manufactureRecordTemplateManager.updateManufactureRecordTemplate(manufactureRecordTemplateBO);
		if (affectedRows > 0) {
			return Result.success();
		}
		return Result.ofErrorRowsAffected();
	}

	@Override
	public Result<?> updateManufactureRecordTemplateStatus(UpdateManufactureRecordTemplateRequestStatus request) {
		ManufactureRecordTemplateBO manufactureRecordTemplateBO = ManufactureRecordTemplateConverter.toBO(request);
		manufactureRecordTemplateBO.setUpdateTime(LocalDateTime.now());
		int affectedRows = manufactureRecordTemplateManager.updateManufactureRecordTemplate(manufactureRecordTemplateBO);
		if (affectedRows > 0) {
			return Result.success();
		}
		return Result.ofErrorRowsAffected();
	}

	@Override
	public Result<?> deleteManufactureRecordTemplate(Long manufactureRecordTemplateId) {
		int affectedRows = manufactureRecordTemplateManager.deleteManufactureRecordTemplate(manufactureRecordTemplateId);
		if (affectedRows > 0) {
			return Result.success();
		}
		return Result.ofErrorRowsAffected();
	}

//	@Override
//	public Result<GetManufactureRecordTemplateResponse> getManufactureRecordTemplate(Long manufactureRecordTemplateId) {
//		GetManufactureRecordTemplateResponse getManufactureRecordTemplateResponse = ManufactureRecordTemplateConverter.toResponse(manufactureRecordTemplateManager.getManufactureRecordTemplate(manufactureRecordTemplateId));
//		Result<GetDataTemplateResponse> dataTemplate = dataTemplateClient.getDataTemplate(GetDataTemplateRequest.builder()
//				.dataTemplateId(getManufactureRecordTemplateResponse.getDataTemplateId())
//				.build());
//		if (!dataTemplate.wasSuccess()) {
//			return Result.ofErrorRowsAffected();
//		}
//		GetDataTemplateResponse getDataTemplateResponse = dataTemplate.getData();
//		//getManufactureRecordTemplateResponse.setGetDataTemplateResponse(getDataTemplateResponse);
//		return Result.success(getManufactureRecordTemplateResponse);
//	}

	@Override
	public List<ManufactureRecordTemplateBO> listManufactureRecordTemplateById(Collection<Long> collection) {
		return manufactureRecordTemplateManager.listManufactureRecordTemplateById(collection);
	}

	@Override
	public List<ManufactureRecordTemplateBO> listManufactureRecordTemplate(ManufactureRecordTemplateQuery query) {
		return manufactureRecordTemplateManager.listManufactureRecordTemplate(query);
	}

	@Override
	public int countManufactureRecordTemplate(ManufactureRecordTemplateQuery query) {
		return manufactureRecordTemplateManager.countManufactureRecordTemplate(query);
	}

//	@Override
//	public Result<GetDataTemplateResponse> saveDataTable(SaveDataTableRequest request) {
//		Result<?> result = dataTemplateClient.saveDataTable(request);
//		if (result.wasSuccess()) {
//			Result<GetDataTemplateResponse> dataTemplate = dataTemplateClient.getDataTemplate(GetDataTemplateRequest.builder()
//					.dataTemplateId(request.getDataTemplateId())
//					.build());
//			if (dataTemplate.wasSuccess()) {
//				return Result.success(dataTemplate.getData());
//			}
//		}
//		return Result.ofNotFound(result.getMsg());
//	}
//
//	@Override
//	public Result<GetDataTemplateResponse> updateDataTable(SaveDataTableRequest request) {
//		Result<?> result = dataTemplateClient.updateDataTable(request);
//		if (result.wasSuccess()) {
//			Result<GetDataTemplateResponse> dataTemplate = dataTemplateClient.getDataTemplate(GetDataTemplateRequest.builder()
//					.dataTemplateId(request.getDataTemplateId())
//					.build());
//			if (dataTemplate.wasSuccess()) {
//				return Result.success(dataTemplate.getData());
//			}
//		}
//		return Result.ofErrorRowsAffected();
//	}
//
//	@Override
//	public Result<GetDataTemplateResponse> saveDataCell(SaveDataCellRequest request) {
//		Result<GetDataCellResponse> result = dataTemplateClient.saveDataCell(request);
//		if (result.wasSuccess()) {
//			Long dataTemplateId = result.getData().getDataTemplateId();
//			Result<GetDataTemplateResponse> dataTemplate = dataTemplateClient.getDataTemplate(GetDataTemplateRequest.builder()
//					.dataTemplateId(dataTemplateId)
//					.build());
//			if (dataTemplate.wasSuccess()) {
//				return Result.success(dataTemplate.getData());
//			}
//		}
//		return Result.ofErrorRowsAffected();
//	}
//
//	@Override
//	public Result<GetDataTemplateResponse> updateDataCell(SaveDataCellRequest request) {
//		Result<GetDataCellResponse> result = dataTemplateClient.updateDataCell(request);
//		if (result.wasSuccess()) {
//			Long dataTemplateId = result.getData().getDataTemplateId();
//			Result<GetDataTemplateResponse> dataTemplate = dataTemplateClient.getDataTemplate(GetDataTemplateRequest.builder()
//					.dataTemplateId(dataTemplateId)
//					.build());
//			if (dataTemplate.wasSuccess()) {
//				return Result.success(dataTemplate.getData());
//			}
//		}
//		return Result.ofErrorRowsAffected();
//	}

    @Override
    public Result<GetManufactureRecordTemplateResponse> getRecordTemplateByDictCode(GetByProductDictCodeRequest request) {
		List<ManufactureRecordTemplateBO> manufactureRecordTemplateBOS = manufactureRecordTemplateManager.listManufactureRecordTemplate(ManufactureRecordTemplateQuery
				.one()
				.productDictCode(request.getProductDictCode())
				.subProductDictCode(request.getSubProductDictCode()).build());
		if (CollectionUtils.isEmpty(manufactureRecordTemplateBOS)) {
			return Result.ofBadRequest("未设置记录模板");
		}
		return Result.success(ManufactureRecordTemplateConverter.toResponse(manufactureRecordTemplateBOS.get(0)));
    }

}
